﻿using Innoapps.FileTransfer.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Innoapps.FileTransfer.Providers.Log
{
    public class EventLog : Base
    {
        override public void WriteLog(Transfer transfer)
        {

            // Get the last transfer event
            ActionResult lastEvent = transfer.ActionResults.Last();

            Type type = lastEvent.GetType();
            PropertyInfo[] properties = type.GetProperties();
            string message = "";

            foreach (PropertyInfo property in properties)
            {
                message += property.GetValue(lastEvent).ToString() + Environment.NewLine;
            }

            System.Diagnostics.EventLog m_EventLog = new System.Diagnostics.EventLog("");
            try
            {
                m_EventLog.Source = "InnoApps.FileTransfer.Core";
                m_EventLog.WriteEntry(message, lastEvent.Result == "Success" ?
                    EventLogEntryType.SuccessAudit : 
                    (lastEvent.Result == "Failure" ? EventLogEntryType.FailureAudit : EventLogEntryType.Error));
            }
            catch (Exception e)
            {
                // Dont do anything if the event logwrite fails.  We dont want the application to fail due to an event log failure.
            }
        }
    }
}
